package com.star.carservice.client.activity;

import butterknife.ButterKnife;
import butterknife.InjectView;

import com.star.carservice.R;
import com.star.carservice.client.activity.base.BaseActivity;

import com.star.carservice.client.fragment.tabs.BoxFragment;
import com.star.carservice.client.fragment.tabs.HomeFragment;
import com.star.carservice.client.fragment.tabs.OrderServiceFragment;
import com.star.carservice.client.fragment.tabs.SettingFragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MainActivity extends BaseActivity {

	@SuppressWarnings("unused")
	private final static String TAG = "MainActivity";

	@InjectView(android.R.id.tabhost)
	FragmentTabHost mTabHost;

	private LayoutInflater mLayoutInflater;

	private int mTabIcons[] = { R.drawable.tab_menu_home,
			R.drawable.tab_menu_order_service, R.drawable.tab_menu_box,
			R.drawable.tab_menu_setting };

	private int mTabTitles[] = { R.string.tab_menu_home,
			R.string.tab_menu_order_service, R.string.tab_menu_box,
			R.string.tab_menu_setting };

	private Class<?> mTabFragments[] = { HomeFragment.class,
			OrderServiceFragment.class, BoxFragment.class,
			SettingFragment.class };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		ButterKnife.inject(this);
		initView();
	}

	private void initView() {
		mLayoutInflater = LayoutInflater.from(this);

		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

		int count = mTabFragments.length;
		for (int i = 0; i < count; i++) {
			TabSpec tabSpec = mTabHost.newTabSpec(getString(mTabTitles[i]))
					.setIndicator(getTabItemView(i));

			mTabHost.addTab(tabSpec, mTabFragments[i], null);

			mTabHost.getTabWidget().getChildAt(i)
					.setBackgroundResource(R.drawable.tab_menu_item_bg);
		}
	}

	/**
	 *
	 * 给每个Tab按钮设置图标和文字
	 */
	@SuppressLint("InflateParams")
	private View getTabItemView(int index) {
		View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
		imageView.setImageResource(mTabIcons[index]);
		
		TextView textView = (TextView) view.findViewById(R.id.textview);
		textView.setText(mTabTitles[index]);
		

		return view;
	}
}
